//
//  BaseTableCell.m
//  SouthChina
//
//  Created by creator_lq on 15/1/27.
//  Copyright (c) 2015年 科创. All rights reserved.
//

#import "BaseTableCell.h"




@implementation BaseTableCell



+(void) addCoreTextMessage:(NSString*)msgText label:(M80AttributedLabel*)label{
    
    [label setText:@""];
    
    if (!msgText) {
        msgText=@"";
    }
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\$\\{[\u4E00-\u9FFF]*\\}"  options:0 error:nil];
    NSArray* matches = [regex matchesInString:msgText
                                      options:NSMatchingWithoutAnchoringBounds
                                        range:NSMakeRange(0, msgText.length)];
    label.lineSpacing = 5.0;
    label.font =[UIFont systemFontOfSize:TEXT_CONTENT_FONT];
    
    
    if (matches.count==0) {
        [label appendText:msgText];
    }
    
    for (int i =0; i<matches.count; i++) {
        
        NSTextCheckingResult* result = [matches objectAtIndex:i];
        NSRange matchRange = [result range];
        if (i==0) {
            if(matchRange.location!=0) {
                NSString *sss =[msgText substringWithRange:NSMakeRange(0, matchRange.location)];
                [label appendText:sss];
            }
        }
        else  {
             NSTextCheckingResult* preResult = [matches objectAtIndex:i-1];
            NSRange preMatchRange = [preResult range];
            if (preMatchRange.location+preMatchRange.length!=matchRange.location) {
                NSString *ddd = [msgText substringWithRange:NSMakeRange(preMatchRange.location+preMatchRange.length, matchRange.location-preMatchRange.location-preMatchRange.length)];
                [label appendText:ddd];
            }
        }
        
        NSDictionary *iconMap= [BaseTableCell getIconMap];;
        UIImage *iamge =[UIImage imageNamed:[iconMap objectForKey:[msgText substringWithRange:NSMakeRange(matchRange.location, matchRange.length)]]];
        if (iamge==nil) {
            iamge = [UIImage imageNamed:@"emji_press"];
        }
        [label appendImage:[UIImage imageNamed:[iconMap objectForKey:[msgText substringWithRange:NSMakeRange(matchRange.location, matchRange.length)]]]
                   maxSize:CGSizeMake(25, 25)
                    margin:UIEdgeInsetsZero
                 alignment:M80ImageAlignmentCenter];
        
         //后面的部分补上
        if (i==(matches.count-1) && matchRange.location+matchRange.length<msgText.length) {
             NSString *ddd = [msgText substringWithRange:NSMakeRange(matchRange.location+matchRange.length, msgText.length-matchRange.location-matchRange.length)];
            [label appendText:ddd];
        }
        
    }
    

}

+(NSDictionary*)getIconMap{
    
    if (iconMap) {
        return iconMap;
    }
    iconMap=[[NSDictionary  alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"expression" ofType:@"plist"]];
    return  iconMap;
}

@end
